//-------------------------------------------------------------------------
// File: handle.hpp
//
// Descr.: - todo -
//
// Author: Stefan Leihkauf
// Date:   07.04.2012
//-------------------------------------------------------------------------

template <class T>
typename handle<T>::value_type handle<T>::InvalidValue = (handle<T>::value_type)0;

//-------------------------------------------------------------------------
/**
*/
template <class T>
handle<T>::handle()
{
}

//-------------------------------------------------------------------------
/**
*/
template <class T>
handle<T>::handle(T h)
{
	_handle.reset(h, handle::close);
}

//-------------------------------------------------------------------------
/**
*/
template <class T>
void handle<T>::reset()
{
	/* if the handle has been previously set (via reset or construction)
		with an value the static close function is automatically involved.
	*/
	_handle.reset();
}

//-------------------------------------------------------------------------
/**
*/
template <class T>
void handle<T>::reset(T h)
{
	_handle.reset(h, handle::close);
}

//-------------------------------------------------------------------------
/**
*/
template <class T>
const T handle<T>::get() const
{
	return static_cast<T>(_handle.get());
}

//-------------------------------------------------------------------------
/**
*/
template <class T>
bool handle<T>::isValid() const
{
	return ((_handle.get() != handle::InvalidValue) && (_handle.get() != INVALID_HANDLE_VALUE));
}

//-------------------------------------------------------------------------
/**
*/
template <class T>
handle<T>::operator bool () const
{
	if((_handle.get() == handle::InvalidValue) || (_handle.get() == INVALID_HANDLE_VALUE))
		return false;

	return true;
}

//-------------------------------------------------------------------------
/**
*/
template <class T>
void handle<T>::close(T& h)
{
	if(h != handle<T>::InvalidValue) {
		detail::HandleCloseVisitor() (h);
		h = (T)handle::InvalidValue;
	}
}